﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LTorrent.Torrent.File
{
    #region BItem
    /// <summary>
    /// integer value
    /// start - 'i'
    /// end - 'e'
    /// Example - i145e => 145
    /// string value
    /// start - length 
    /// end -
    /// Example 5:hello => "hello"
    /// </summary>
    public class BItem
    {
        protected byte[] strValue = null;
        protected int intValue = 0;
        protected bool IsInt = true;
        
        public byte[] GETvalue
        {
            get { return strValue; }
        }
        public bool isInt
        {
            get
            {
                return  IsInt;
            }
        }
        public BItem(byte[] A)
        {
            strValue = A;
            IsInt = false;
        }
        public BItem(int A)
        {
            IsInt = true;
            intValue = A;
        }
        public string ToString()
        {
            if (IsInt)
                return intValue.ToString();
            return Encoding.ASCII.GetString(strValue);
        }
        public void Serialize(BinaryWriter outStream)
        {

            if (isInt)
            {
                char[] Value = intValue.ToString().ToCharArray();
                outStream.Write('i');
                outStream.Write(Value);
                outStream.Write('e');
            }
            else
            {
                char[] LStr = strValue.Length.ToString().ToCharArray();
                outStream.Write(LStr);
                outStream.Write(':');
                outStream.Write(strValue);
            }
        }
    }
    #endregion
}
